﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.BusinessLayer;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.UserControls.Selections
{
    /// <summary>
    /// Employee simple selection.
    /// </summary>
    /// <remarks></remarks>
    public partial class EmployeeSimpleSelection : System.Web.UI.UserControl
    {
        /// <summary>
        /// Gets or sets a value indicating whether [public auto postback].
        /// </summary>
        /// <value><c>true</c> if [public auto postback]; otherwise, <c>false</c>.</value>
        /// <remarks></remarks>
        public bool PublicAutoPostback
        {
            get
            {
                return RadComboBoxEmployee.AutoPostBack;
            }
            set
            {
                RadComboBoxEmployee.AutoPostBack = value;
            }
        }

        /// <summary>
        /// selected employee ID.
        /// </summary>
        private int? _selectedUserID;
        /// <summary>
        /// Gets or sets the selected user ID.
        /// </summary>
        /// <value>The selected user ID.</value>
        /// <remarks></remarks>
        public int? SelectedUserID 
        { 
            get
            {
                if (RadComboBoxEmployee.SelectedValue == "")
                {
                    return null;
                }
                else
                {
                    return Int32.Parse(RadComboBoxEmployee.SelectedValue);// _selectedUserID;
                }
            }
            set
            {
                if (value != null)
                {
                    _selectedUserID = value;
                }
            }
        }


        /// <summary>
        /// Occurs when [event employee changed].
        /// </summary>
        /// <remarks></remarks>
        public event EventHandler EventEmployeeChanged;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateEmployees(null);

            if (_selectedUserID != null)
            {
                RadComboBoxEmployee.SelectedValue = _selectedUserID.ToString();// SelectedUserID.ToString();
            }
        }

        /// <summary>
        /// Populates the employees2.
        /// </summary>
        /// <param name="users">The users.</param>
        /// <remarks></remarks>
        private void PopulateEmployees2(List<User> users)
        {
            RadComboBoxEmployee.DataSource = users;
            
            RadComboBoxEmployee.DataBind();
        }

        /// <summary>
        /// Populates the employees.
        /// </summary>
        /// <param name="SBU_ID">The SB u_ ID.</param>
        /// <remarks></remarks>
        private void PopulateEmployees(int? SBU_ID)
        {
            UserManager um = new UserManager();

            if (SBU_ID == null)
            {
                RadComboBoxEmployee.DataSource = um.GetAllUsers();
            }
            else
            {
                RadComboBoxEmployee.DataSource = um.GetAllUsersByDepartment((int)SBU_ID);
            }
            RadComboBoxEmployee.DataBind();
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the RadComboBoxEmployee control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        public void RadComboBoxEmployee_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (EventEmployeeChanged != null)
            {
                EventEmployeeChanged(sender, e);
            }
        }

        /// <summary>
        /// Sets the selected employee.
        /// </summary>
        /// <param name="userID">The user ID.</param>
        /// <remarks></remarks>
        public void SetSelectedEmployee(int? userID)
        {
            if (userID != null)
            {
                RadComboBoxEmployee.SelectedValue = SelectedUserID.ToString();
            }
        }
    }
}